<script>
import playIcon from "../assets/play_black.png";
import stopIcon from "../assets/stop_black.png";
import pauseIcon from "../assets/pause_black.png";

export default {
  name: "timer",
  data() {
    return {
      playIcon,
      stopIcon,
      pauseIcon,
      time: 0, //计时器时长，秒数
      timer: null, //计时器
    };
  },
  computed: {
    hour() {
      return String(Math.floor(this.time / 3600)).padStart(2, "0");
    },
    minute() {
      return String(Math.floor((this.time % 3600) / 60)).padStart(2, "0");
    },
    second() {
      return String(this.time % 60).padStart(2, "0");
    },
  },
  methods: {
    //开始计时
    startTimer() {
      this.timer = setInterval(() => {
        this.time++;
      }, 1000);
    },
    //暂停计时
    pauseTimer() {
      clearInterval(this.timer);
      this.timer = null;
    },
    //停止计时
    stopTimer() {
      clearInterval(this.timer);
      this.time = 0;
      this.timer = null;
    },
  },
  beforeDestroy() {
    this.stopTimer();
  },
  mounted() {
    this.startTimer();
  },
};
</script>

<template>
  <view class="component">
    <view class="content">
      <view class="left side">
        <view class="title">解题用时</view>
        <view class="time">
          <text>{{ hour }}</text>
          <text>:</text>
          <text>{{ minute }}</text>
          <text>:</text>
          <text>{{ second }}</text>
        </view>
      </view>
      <view class="right side">
        <!--				暂停 / 播放按钮-->
        <view class="btn">
          <image
            class="icon"
            :src="playIcon"
            v-if="!timer"
            @click="startTimer"
          ></image>
          <image
            class="icon"
            :src="pauseIcon"
            v-else
            @click="pauseTimer"
          ></image>
        </view>
        <!--				停止按钮-->
        <view class="btn">
          <image class="icon" :src="stopIcon" @click="stopTimer"></image>
        </view>
      </view>
    </view>
  </view>
</template>

<style scoped lang="scss">
.component {
  padding: 20rpx;
  background: white;
  .content {
    display: flex;
    justify-content: space-between;
    border-radius: 20rpx;
    overflow: hidden;
    height: 84rpx;
    .side {
      display: flex;
      align-items: center;
      height: 100%;
    }
    .left {
      padding-left: 40rpx;
      width: 0;
      flex: 1;
      background: var(--theme-color-light);
      .title {
        font-size: 34rpx;
        color: var(--text-color-theme);
      }
      .time {
        font-size: 34rpx;
        font-weight: bold;
        margin-left: 40rpx;
      }
    }
    .right {
      .btn {
        background: var(--theme-color-light);
        margin-left: 10rpx;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        width: 120rpx;
        .icon {
          width: 40rpx;
          height: 40rpx;
        }
      }
    }
  }
}
</style>
